/* eslint-disable max-classes-per-file */
/*
 * @Description:
 * @Version: 2.0
 * @Autor: wiz
 * @Date: 2023-08-15 13:02:23
 * @LastEditors: swc
 * @LastEditTime: 2024-08-05 15:31:32
 */
import { N_oSetupPanel } from '../../types';
import { T_compPanelConf } from '../compConfPanel';
import { T_toolbarLib } from '../compToolbar';
import { T_compBase } from './compBase';

type I_tableStyle = N_oSetupPanel.N_comps.I_tableStyle;
type I_compToolbar = N_oSetupPanel.N_comps.I_compToolbar;
type I_messageConf = N_oSetupPanel.N_comps.I_messageConf;
export interface I_formPanel {
  label: string;
  content: {
    name: string;
    formStyle: {
      // columnNum: number;
      // pixelWidth: number;
      // percentageWidth: number;
      isBottom?: boolean;
    };
    formColumns: Array<any>;
    uniqueIndexes: Array<any>;
    indexes: Array<any>;
    rules: Array<any>;
    dbType: string;
    // mainFun: string;
  };
}
export interface I_tablePanel {
  label: string;
  content: {
    name: string;
    dbType: string;
    tableStyle: I_tableStyle;
    tableColumns: Array<any>;
    uniqueIndexes: Array<any>;
    indexes: Array<any>;
    rules: Array<any>;
    action: object;
    headerGroup: Array<any>;
    charts: Array<any>;
    toolbar: I_compToolbar;
    tableDataSetting: {
      editable: boolean;
      autoload: boolean;
    };
  };
}
export class T_cloneBasePanelConf {
  name: string;

  comment: string;

  refTable: string; // 引用功能

  dbType?: 'mysql' | 'mongo';

  isCreated?: boolean;

  status?: string;

  tableStyle?: I_tableStyle;

  // 表格样式
  toolbar?: I_compToolbar;

  messageConfig?: I_messageConf;

  selbill?: Array<any>;

  queryLink?: Array<any>;

  formPanels?: Array<I_formPanel>;

  tablePanels?: Array<I_tablePanel>;

  // 唯一索引
  indexes?: Array<any>;

  uniqueIndexes?: Array<any>;

  action?: object;
}

export class T_cloneBasePanel extends T_compBase {
  private TtoolbarLib: T_toolbarLib = new T_toolbarLib();

  private TcompPanelConf: T_compPanelConf = new T_compPanelConf();

  public conf: T_cloneBasePanelConf = new T_cloneBasePanelConf();

  constructor() {
    super();
    this.conf = {
      name: '',
      comment: '',
      refTable: '',
      isCreated: false,
      status: '',
      dbType: N_oSetupPanel.N_comps.DB_TYPE.MYSQL
      // toolbar: null,
      // messageConfig: {
      //   custDefListener: {
      //     search: [],
      //     coordination: null
      //   },
      //   emitActions: {
      //     search: [],
      //     refresh: []
      //   },
      //   eventGroup: null
      // },
      // selbill: [],
      // queryLink: [],
      // indexes: [], // 索引
      // uniqueIndexes: [],
      // formPanels: [
      //   {
      //     label: '',
      //     content: {
      //       name: '',
      //       formStyle: {
      //         // columnNum: 0,
      //         // pixelWidth: 0,
      //         // percentageWidth: 0,
      //         isBottom: false
      //       },
      //       formColumns: [],
      //       uniqueIndexes: [],
      //       indexes: [],
      //       rules: [],
      //       dbType: ''
      //     }
      //   }
      // ],
      // tablePanels: [
      //   {
      //     label: '',
      //     content: {
      //       name: '',
      //       dbType: '',
      //       tableStyle: {
      //         showHeader: true,
      //         showFooter: true,
      //         // showPager: false,
      //         showToolbar: true,
      //         border: true,
      //         // eslint-disable-next-line no-use-before-define
      //         mode: N_oSetupPanel.N_comps.TABLESTYLE_MODE.MODE_NULL
      //       },
      //       tableColumns: [],
      //       uniqueIndexes: [],
      //       indexes: [],
      //       rules: [],
      //       action: {},
      //       headerGroup: [],
      //       charts: [],
      //       // toolbar: I_compToolbar;
      //       toolbar: {
      //         // 待做配置
      //         default: true,
      //         insert: true,
      //         delete: true,
      //         save: false,
      //         import: true,
      //         export: true,
      //         globalSearch: true,
      //         edit: true,
      //         print: true,
      //         refresh: false,
      //         addon: []
      //       },
      //       tableDataSetting: {
      //         editable: true,
      //         autoload: true
      //       }
      //     }
      //   }
      // ],
      // action: {}
    };
    // this.conf.toolbar = this.TtoolbarLib.getCompToolBarSet(
    //   N_oSetupPanel.N_comps.COMP_NAME.BASE_PANEL
    // );
  }

  public addSelbill(...p: any[]): void {
    // this.conf.selbill.push(...p);
  }

  public addQueryLink(...p: any[]): void {
    // this.conf.queryLink.push(...p);
  }

  public addUniqueIndexes(...p: any[]): void {
    // this.conf.uniqueIndexes.push(...p);
  }

  public getConf(): any {
    return this.conf;
  }

  public getComponentId(): N_oSetupPanel.N_comps.COMP_MODULE_ID {
    return N_oSetupPanel.N_comps.COMP_MODULE_ID.CLONE_BASE_PANEL;
  }

  public GetCompPanelConf(): N_oSetupPanel.N_comps.I_setupPanel {
    return this.TcompPanelConf.getCompPanelSet(
      N_oSetupPanel.N_comps.COMP_NAME.CLONE_BASE_PANEL
    );
  }

  public getPermissionList(
    config: T_cloneBasePanelConf
  ): Array<N_oSetupPanel.I_permission> {
    return [
      {
        type: 'button',
        area: 'toolbar',
        data: this.getToolbarKey(config)
      },

      {
        type: 'selbill',
        area: 'toolbar',
        data: this.getSelbillData(config)
      },
      {
        type: 'customButton',
        area: 'toolbar',
        data: this.getPmsAddonData(config)
      },
      {
        type: 'columns',
        area: 'form',
        data: this.getPmsForm(config)
      },
      {
        type: 'columns',
        area: 'table',
        data: this.getPmsColumn(config)
      }
    ];
  }

  private getToolbarKey(config: any): Array<string> {
    if (config?.tableColumns) {
      return ['insert', 'delete', 'edit'];
    }
    if (config?.formPanels) {
      // return Object.keys(this.conf.toolbar);
      const tools = Object.keys(config.toolbar);
      if (tools.indexOf('addon') > -1) {
        tools.splice(tools.indexOf('addon'), 1);
      }
      return tools;
    }
    return [];
  }

  private getSelbillData(config: T_cloneBasePanelConf): Array<string> {
    return [];
  }

  private getPmsColumn(config: any): Array<string> {
    const arr: Array<string> = [];
    // if (config?.tablePanels) {
    //   config.tablePanels.forEach(tablePanel => {
    //     tablePanel.content.tableColumns.forEach(item => {
    //       arr.push(item.field);
    //     });
    //   });
    // } else
    if (config?.tableColumns) {
      config.tableColumns.forEach(item => {
        arr.push(item.field);
      });
    }

    return arr;
  }

  private getPmsForm(config: any): Array<string> {
    const arr: Array<string> = [];
    if (config?.formPanels) {
      config.formPanels.forEach(formPanel => {
        formPanel.content.formColumns.forEach(item => {
          arr.push(item.field);
        });
      });
    } else if (config?.formColumns) {
      config.formColumns.forEach(item => {
        arr.push(item.field);
      });
    }

    return arr;
  }

  private getPmsAddonData(config: T_cloneBasePanelConf): Array<string> {
    const arr: Array<string> = [];
    if (Array.isArray(config?.toolbar?.addon)) {
      config.toolbar.addon.forEach(ctm => {
        arr.push(ctm.title);
        if (ctm.children.length > 0) {
          // eslint-disable-next-line no-use-before-define
          handleAddonData(ctm.children);
        }
      });
    }

    function handleAddonData(children): void {
      for (const child of children) {
        arr.push(child.title);
        if (child.children.length > 0) {
          handleAddonData(child.children);
        }
      }
    }
    return arr;
  }
}
